﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Data.Model;
using NHibernate;
using DotNet.Common.NHibernateUtil;
using ZLCMS.Business.Util;
using ZLCMS.Business.DwzFrame;

namespace ZLCMS.Web.SystemManage.Template.Template
{
    public partial class PageDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                int uid = 0;
                try
                {
                    uid = Int32.Parse(Request["uid"]);
                }
                catch
                {

                }
                CmsPageInfo page = null;
                using (ISession session = SessionFactoryHelper.CreateSession())
                {
                    if (uid > 0)
                    {
                        page = session.Get<CmsPageInfo>(uid);
                    }
                }
                if (page != null)
                {
                    name.Text = page.Name;
                    sort.Text = page.Sort.ToString();
                    memo.Text = page.Memo;
                    template1.Value = page.TemplateData;
                    previewurl.Value = page.PreviewUrl;
                    //is_default.Checked = page.IsDefault == "1";
                    thumburl.Value = page.ThumbnailUrl;
                    imgurl.Value = page.ImageUrl;
                    ViewState["uid"] = uid;
                }
                else 
                {
                    page = new CmsPageInfo();
                }
                ParamKit.Bind("template_pages_kind", kind, null, new List<ListItem>() { ParamKit.ParamItemEx_QXZ() }, null, page.Kind, "-1");
            }
            else
            {
                int uid = ViewState["uid"] == null ? 0 : (int)ViewState["uid"];

                CmsPageInfo page = null;
                using (ISession session = SessionFactoryHelper.CreateSession())
                {
                    page = session.Get<CmsPageInfo>(uid);
                    if (page == null)
                    {
                        page = new CmsPageInfo();
                    }

                    page.Name = name.Text;
                    try
                    {
                        page.Sort = Int32.Parse(sort.Text);
                    }
                    catch 
                    {

                    }
                    //page.IsDefault = is_default.Checked ?"1":"0";
                    page.ThumbnailUrl = thumburl.Value;
                    page.ImageUrl = imgurl.Value;
                    page.Memo = memo.Text;
                    page.TemplateData = template1.Value;
                    page.Kind = kind.SelectedValue;
                    page.DocKind = "Template";
                    page.PreviewUrl = previewurl.Value;
                    session.SaveOrUpdate(page);
                    session.Flush();
                }


                FrameHelper _framePage = new FrameHelper(this);
                ReturnCode rc = new ReturnCode()
                {
                    statusCode = 200,
                    message = "操作成功"
                };

                rc.callbackType = "forward";
                rc.forwardUrl = "/SystemManage/Template/Template/PageDetail.aspx?tabid=PageDetail";

                if (Request["submit_op"] != "1")
                {
                    rc.forwardUrl += "&uid=" + page.Id;
                }

                _framePage.Alert(rc);
            }

        }
    }
}